#pragma once

#include <ai/AI.h>
using namespace std;

//==================================
//De kiem tra cac dinh lan can
//Khong dung so 0 vi co dinh thu 0
#define p_NONE			-1
#define p_TOP			1
#define p_DOWN			2
#define p_LEFT			3
#define p_RIGHT			4
#define p_TOP_LEFT		5
#define p_TOP_RIGHT		6
#define p_DOWN_LEFT		7
#define p_DOWN_RIGHT	8
//=================================

struct Area
{
	int vert;
	int area;
	Area() : vert(0), area(0) {}
	Area(int _vert, int _area) : vert(_vert), area(_area) {}
	Area operator =(Area ar) {vert = ar.vert; area = ar.area; return *this;}
};

class GodOfAI
{
private:
	int size;//Map size
	int s_size;// size of ma tran ke	
	int* data;// mang gia tri cua MAP

	int* mainLine;// duong di se di
	bool hasMainLine;
	int countMainLine;

	int currentVert;
	int currentEnemyVert;
	Position currentPos;
	
	int** saveAllLine;
	int* countSaveAllLine;// co the ko dung
	int countAllLine;

	int* saveLine; //luu dinh da di qua

	bool hasADeadLine;
	int* saveADeadLine;

	int* temp_Data;

	Area* s_Area;//Phan vung
	int lengthOfArea;

	int indexTheBestLine;
	int enemyFreeVert;
	int myFreeVert;

	bool hasLiveToEnemy;

	static GodOfAI* s_Instance;

	//------------------------------------------

	void UpdateMainLine();

	int GetDirection(int vert);

	void ClearVertices(int* arrVert);
	int GetVerticeFromPosition(Position pos);
	Position GetPositionFormVertice(int vert);
	int GetLevelLiveVert(int vert);
	int GetLevelLiveVert(int vert, int* arrData);
	int GetLevelDeadVert(int vert);
	int GetVerticeAround(int vert, int pos);

	bool HasMarkVertice(int vert, int* arrVert);

	void TryFindAEndLine(int vStart, int count);
	bool IsEndLineCanDiv(int endLine);
	void TryFindAllLine(int vStart, int count);

	void TryFindVertice(int startVert, int endVert);

	void TryFindArea(int vStart, int areaIndex);
	void ClearArea();
	bool WasVerticeArea(int vert);
	bool WasAllVerticeArea(int &otherVerticeZone);
	void SetAreaForVerticeWent(int areaIndex);
	void SetArea();	

	void FindMaxLine();
	void TryFindMaxLine(int vStart, int count);

	void MakeTempData(int* arrData, int length);

	void MakeTheBestLine();
	
	
	//int** s_matrix;// mang 2 chieu ma tran ke
	//void CreateMatrix();
	

public:
	GodOfAI(int size);

	//Lay duong di
	int GetDirection();

	static GodOfAI* GetInstance();
};